package com.o2o.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateTools {
	private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	private static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH");
	public static String addDay(String dateString, int addDay) {
		try {
			Calendar cd = Calendar.getInstance();
			cd.setTime(sdf.parse(dateString));
			cd.add(Calendar.DATE, addDay);// 增加一天
			// cd.add(Calendar.MONTH, n);//增加一个月

			return sdf.format(cd.getTime());
		} catch (Exception e) {
			System.out.println("time format error");
			return null;
		}
	}
	public static String getSqlDateFormat(String startTime,String endTime) {
		try {
			long time = sdf.parse(endTime).getTime() -sdf.parse(startTime).getTime();
			String sqlDateFormat = "";
			if(time > 3*24*3600*1000){
				sqlDateFormat = "%Y-%m-%d";
			}else{
				sqlDateFormat = "%Y-%m-%d %H";
			}
			return sqlDateFormat;
		} catch (Exception e) {
			System.out.println("sql Dateformat error");
			return null;
		}
	}
	public static Long getFormatDate(String date) {
		try {
			long time;
			if (date.contains(" ")){
				 time = format.parse(date).getTime();
			}else{
				 time = sdf.parse(date).getTime();
			}
			return time;
		} catch (Exception e) {
			System.out.println("different format error");
			return null;
		}
	}
	
	public static String getNow(){
		SimpleDateFormat smt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return smt.format(new Date());
	}
}
